import 'package:changxindianzishangwu/cache/local_cache.dart';
import 'package:changxindianzishangwu/http/base/ec_base_request.dart';
import 'package:changxindianzishangwu/http/request/data_center_conversation_count_request.dart';
import 'package:changxindianzishangwu/http/request/data_center_friends_count_request.dart';
import 'package:changxindianzishangwu/http/request/data_center_overview_request.dart';
import 'package:changxindianzishangwu/http/request/data_center_receiver_msg_count_request.dart';
import 'package:changxindianzishangwu/http/request/data_center_send_msg_count_request.dart';
import 'package:changxindianzishangwu/http/zd_net.dart';
import 'package:changxindianzishangwu/model/base_model.dart';
import 'package:changxindianzishangwu/model/data_center_overview_model.dart';
import 'package:changxindianzishangwu/model/data_center_list_model.dart';
import 'package:common_utils/common_utils.dart';

class DataCenterDao {
  static getDataOverview(Duration startDate,
      {String startTime, String endTime}) async {
    String endData = ObjectUtil.isEmptyString(endTime)
        ? DateUtil.formatDate(DateTime.now(), format: DateFormats.y_mo_d)
        : endTime;
    String startData = ObjectUtil.isEmptyString(startTime)
        ? DateUtil.formatDate(DateTime.now().subtract(startDate),
            format: DateFormats.y_mo_d)
        : startTime;
    EcBaseRequest request = DataCenterOverviewRequest();
    request
        .add("access_token", getAppLoginToken())
        .add("endDate", endData)
        .add("startData", startData)
        .add("level", getUserInfo().level)
        .add("userId", getUserId());
    var result = await ZdNet.getInstance().send(request);
    if(result["resultCode"]!=1){
      return BaseModel.fromJson(result);
    }else{
      DataCenterOverviewModel _model = DataCenterOverviewModel.fromJson(result);
      return _model;
    }

  }

  static getDataList(startDate, int index) async {
    //index 0新增好友 1会话人数 2发送消息 3接收消息  本地判断
    String endData =
        DateUtil.formatDate(DateTime.now(), format: DateFormats.y_mo_d);
    String startData = DateUtil.formatDate(DateTime.now().subtract(startDate),
        format: DateFormats.y_mo_d);
    EcBaseRequest request = index == 0
        ? DataCenterFriendsCountRequest()
        : index == 1
            ? DataCenterConversationCountRequest()
            : index == 2
                ? DataCenterSendMsgCountRequest()
                : DataCenterReceiveMsgCountRequest();
    request
        .add("access_token", getAppLoginToken())
        .add("endDate", endData)
        .add("startData", startData)
        .add("level", getUserInfo().level)
        .add("timeUnit", "2")
        .add("userId", getUserId());
    Map result = await ZdNet.getInstance().send(request);
    int code = result["resultCode"];
    print('返回数据 类型：$index  数据：$result');
    if (code == 1) {
      DataCenterListModel _model = DataCenterListModel.fromJson(result);
      return _model.data;
    }
    return code;
  }
}
